package ch.escaflowne.mangalist;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class addNewManga extends Activity
{
	public static final String INTENT_PARAMETER_TYPE = "TYPE";
	public static final String INTENT_PARAMETER_TITLE = "TITLE";
	public static final String INTENT_PARAMETER_AUTOR = "AUTOR";
	public static final String INTENT_PARAMETER_VOLUMES = "VOLUMES";
	public static final String INTENT_PARAMTERE_EDIT_ID = "ID";

	public static final String INTENT_TYPE_NEW_MANGA = "ADD_NEW_MANGA";
	public static final String INTENT_TYPE_EDIT_MANGA = "EDIT_EXISTING_MANGA";
	
	EditText title, autor, totvol;
	Button addeditbutton;
	Toast toast;
	int _id = -1;
	
	@Override
	 public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		drawUI();
			
		//final Button addNewMangaButton = (Button)findViewById(R.id.button1);
		addeditbutton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (title.getText().length() == 0 || autor.getText().length() == 0 || totvol.getText().length() == 0)
				{	// information incomplete
					toast = Toast.makeText(getApplicationContext(), "One or more Fields are empty! Please fill them to proceed", Toast.LENGTH_SHORT);
					toast.show();
				}
				else
				{
					//manga.addManga(title.getText().toString(), autor.getText().toString(), Integer.parseInt(totvol.getText().toString()));
					//manga.addManga("Yotsuba&!", "Azuma Kyohiko", 10);
					
					Intent intent = new Intent();
					intent.putExtra(INTENT_PARAMETER_TITLE,		title.getText().toString());
					intent.putExtra(INTENT_PARAMETER_AUTOR,		autor.getText().toString());
					intent.putExtra(INTENT_PARAMETER_VOLUMES,	Integer.parseInt(totvol.getText().toString()));
					intent.putExtra(INTENT_PARAMTERE_EDIT_ID, _id);
					if (getIntent().hasExtra(INTENT_PARAMETER_TYPE))
						intent.putExtra(INTENT_PARAMETER_TYPE, getIntent().getStringExtra(INTENT_PARAMETER_TYPE));
					setResult(RESULT_OK, intent);
					finish();
				}
			}
		});
	}
	
	private void drawUI()
	{
		Intent intent = getIntent();
		
		setContentView(R.layout.addnewmanga);

		title = (EditText)findViewById(R.id.add_title);
		autor = (EditText)findViewById(R.id.add_autor);
		totvol = (EditText)findViewById(R.id.add_totvol);
		addeditbutton = (Button)findViewById(R.id.button1);
		
		if (intent.hasExtra(INTENT_PARAMETER_TYPE))
			if (intent.getStringExtra(INTENT_PARAMETER_TYPE).compareTo(INTENT_TYPE_EDIT_MANGA) == 0)
			{
				title.setText(intent.getStringExtra(INTENT_PARAMETER_TITLE));
				autor.setText(intent.getStringExtra(INTENT_PARAMETER_AUTOR));
				totvol.setText(intent.getStringExtra(INTENT_PARAMETER_VOLUMES));
				_id = intent.getIntExtra(INTENT_PARAMTERE_EDIT_ID, -1);
				addeditbutton.setText("@+id/edit_manga");
			}
	}
}
